$(function () {
  // set breadcrumb title
  $("li.breadcrumb-title").html("应用管理");
  $("#breadcrumb-app-center-div").hide();
  $("#ddal-app-center").selectpicker().ajaxSelectPicker({
    ajax: {
      contentType: "application/json;charset=UTF-8",
      url: $.serviceUrl.appCenter.list,
      type: 'POST',
      dataType: 'json',
      // Use "{{{q}}}" as a placeholder and Ajax Bootstrap Select will
      // automatically replace it with the value of the search query.
      data: {'name': '{{{q}}}'},
      extendData:
        [
            {
              key: "hello",
              func: function() {
                  return $("#ddal-app-name").val();
                }
            }
        ]
    },

    locale: {
      emptyTitle: '选择业务中心'
    },
    log: 3,
    preprocessData: function (data) {
      var i, l = data.length, array = [];
      if (l) {
        for (i = 0; i < l; i++) {
          array.push($.extend(true, data[i], {
            text: data[i].name,
            value: data[i].code,
            data: {
              subtext: data[i].code
            }
          }));
        }
      }
      // You must always return a valid array when processing data. The
      // data argument passed is a clone and cannot be modified directly.
      return array;
    }
  });

  $("#bizapp-btn-submit").click(function() {
    // if(! requiredCheck()) {
    //   return;
    // }

    var appName = $("#ddal-app-name").val();
    if (!appName) {
      notie.alert({
        type: 3,
        text: "请填写应用名称",
        stay: false,
        time: 3,
        position: "top"
      });
      return;
    }

    // check center selection
    var selectedCenter = $("select#ddal-app-center").find(":selected").val();
    if (!selectedCenter) {
      notie.alert({
        type: 3,
        text: "请选择应用归属中心",
        stay: false,
        time: 3,
        position: "top"
      });
      return;
    }

    var monitorUrl = $("#ddal-app-monitor-url").val();
    if (!monitorUrl) {
      notie.alert({
        type: 3,
        text: "请填写应用的监控地址",
        stay: false,
        time: 3,
        position: "top"
      });
      return;
    }

    var $data = {
      "appName": appName,
      "center": selectedCenter,
      "adminUrl": monitorUrl
    };
    $.ajax({
      type: 'POST',
      url: $.serviceUrl.app.create,
      dataType: 'json',
      cache: false,
      async: true,
      data: JSON.stringify($data),
      contentType: "application/json;charset=UTF-8",
      success: function (data) {
        notie.alert({
          type: 1,
          text: "应用 [" + data.appName + "] 创建成功, 请在",
          stay: false,
          time: 3,
          position: "top"
        });
        // clearCreateInputs();
        // resetInputWarning();
        // load first page
        // loadCenterList(1);
      },
      error: function (data) {
        if (data) {
          notie.alert({
            type: 3,
            text: data.responseJSON.message,
            stay: false,
            time: 3,
            position: "top"
          });
        }
      }
    });
  });

  function requiredCheck() {
    var $passed = true;
    $("input.required").each(function () {
      if (!$(this).val()) {
        var $this = $(this);
        var $pDiv = $("#" + $this.data("pdiv"));
        if ($pDiv) {
          $pDiv.addClass("has-danger");
        }
        $this.addClass("form-control form-control-danger");
        $passed = false;
      }
    });
    return $passed;
  }
  // $('select').trigger('change');
});